package org.realogic.btp.dhtmlx.dao;

import java.net.URL;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.realogic.btp.dhtmlx.define.DhtmlxGridField;
import org.realogic.btp.dhtmlx.define.DhtmlxGridModel;
import org.realogic.btp.dhtmlx.define.DhtmlxUserData;
import org.realogic.cfm.util.UtilXml;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;

public class DhtmlxGridDefineDAO {
	private String filePathName;
	private DhtmlxGridModel model;

	public DhtmlxGridDefineDAO(String theFileName) {
		filePathName = theFileName;
	}

	public DhtmlxGridModel getModel() {
		return model;
	}

	public void setModel(DhtmlxGridModel model) {
		this.model = model;
	}

	private Element getRoot() {
		Element root = null;
		try {
			URL url = new URL("file:///" + filePathName);
			// Thread.currentThread().getContextClassLoader().getResource("dhtmxgrid\\"
			// + fileName + ".xml");
			root = loadDocument(url);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return root;
	}

	private Element loadDocument(URL url) {
		Document doc = null;
		try {
			InputSource xmlInp = new InputSource(url.openStream());
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder parser = docBuilderFactory.newDocumentBuilder();
			doc = parser.parse(xmlInp);
			Element root = doc.getDocumentElement();
			root.normalize();
			return root;
		} catch (Exception pce) {
			System.err.println("DhtmlxGridDefineDAO error: " + pce);
		}
		return null;
	}

	public void doParse() {
		Element tmpRoot = getRoot();
		if (tmpRoot == null) {
			return;
		}
		load(tmpRoot);
	}

	private void load(Element element) {
		List<Element> flagElementList = UtilXml.childElementList(element, "column");
		model = new DhtmlxGridModel((flagElementList.size() * 4) / 3 + 1);
		String isAuth = element.getAttribute("isAuth");
		if (isAuth == "" || isAuth == null) {
			model.setAuth(false);
		} else {
			model.setAuth(Boolean.parseBoolean(isAuth));
		}
		Element flagElement = null;
		DhtmlxGridField dhtmlxGridHeader = null;
		String tmpRef = null;
		if (flagElementList != null) {
			Iterator<Element> flagElementIt = flagElementList.iterator();
			while (flagElementIt.hasNext()) {
				dhtmlxGridHeader = new DhtmlxGridField();
				flagElement = flagElementIt.next();
				tmpRef = flagElement.getAttribute("ref").trim();
				if (tmpRef.length() > 0) {
					DhtmlxGridRefDefineDAO dao = new DhtmlxGridRefDefineDAO(tmpRef, flagElement.getAttribute("prex").trim());
					List<DhtmlxGridField> tmpList = dao.doParse();
					if (tmpList != null) {
						int size = tmpList.size();
						for (int i = 0; i < size; i++) {
							model.add(tmpList.get(i));
						}
					}
				} else {
					dhtmlxGridHeader.setDescription(flagElement.getTextContent().trim());
					dhtmlxGridHeader.setWidth(flagElement.getAttribute("width").trim());
					dhtmlxGridHeader.setType(flagElement.getAttribute("type").trim());
					dhtmlxGridHeader.setAlign(flagElement.getAttribute("align").trim());
					dhtmlxGridHeader.setSort(flagElement.getAttribute("sort").trim());
					dhtmlxGridHeader.setColor(flagElement.getAttribute("color").trim());
					dhtmlxGridHeader.setFormat(flagElement.getAttribute("format").trim());
					dhtmlxGridHeader.setHidden(flagElement.getAttribute("hidden").trim());
					dhtmlxGridHeader.setAttribute(flagElement.getAttribute("attribute").trim());
					dhtmlxGridHeader.setColumnID(flagElement.getAttribute("columnid").trim());
					if (dhtmlxGridHeader.getColumnID().equals("")) {
						dhtmlxGridHeader.setColumnID(dhtmlxGridHeader.getAttribute());
					}
					dhtmlxGridHeader.setAuth(flagElement.getAttribute("auth").trim());
					dhtmlxGridHeader.setFooter(flagElement.getAttribute("footer").trim());
					dhtmlxGridHeader.setStat(flagElement.getAttribute("stat").trim());
					dhtmlxGridHeader.setExcelStyle(flagElement.getAttribute("excelStyle").trim());
					model.add(dhtmlxGridHeader);
				}
			}
		}

		List<Element> tmpList = UtilXml.childElementList(element, "userdata");
		Element tmpElement = null;
		DhtmlxUserData dhtmlxUserData = null;
		if (tmpList != null) {
			Iterator<Element> tmpElementIt = tmpList.iterator();
			while (tmpElementIt.hasNext()) {
				dhtmlxUserData = new DhtmlxUserData();
				tmpElement = tmpElementIt.next();
				dhtmlxUserData.setName(tmpElement.getAttribute("name").trim());
				dhtmlxUserData.setAttribute(tmpElement.getAttribute("attribute").trim());
				dhtmlxUserData.setColumnID(tmpElement.getAttribute("columnid").trim());
				dhtmlxUserData.setAuth(tmpElement.getAttribute("auth").trim());
				model.add(dhtmlxUserData);
			}
		}

		List<Element> tmpGlobalList = UtilXml.childElementList(element, "globaluserdata");
		if (tmpGlobalList != null) {
			Iterator<Element> tmpElementIt = tmpGlobalList.iterator();
			while (tmpElementIt.hasNext()) {
				dhtmlxUserData = new DhtmlxUserData();
				tmpElement = tmpElementIt.next();
				dhtmlxUserData.setDescription(tmpElement.getTextContent().trim());
				dhtmlxUserData.setFormat(tmpElement.getAttribute("format").trim());
				dhtmlxUserData.setName(tmpElement.getAttribute("name").trim());

				dhtmlxUserData.setAttribute(tmpElement.getAttribute("attribute").trim());
				dhtmlxUserData.setAuth(tmpElement.getAttribute("auth").trim());
				model.addGlobal(dhtmlxUserData);
			}
		}
	}
}
